<template>
  <div>
    <h4>发表评论</h4>
    <van-divider />
    <div class>
      <textarea v-model="text23" cols="30" rows="10"></textarea>
      <van-button type="info" @click="addComent">发表评论</van-button>
    </div>
    <van-list>
      <van-cell v-for="(item,i) in commentList" :key="item.id">
        <div class="title">
          <span>第{{i+1}}楼</span>
          <span>用户：{{item.user_name}}</span>
          <span>发表时间：{{(item.add_time|| '').split('T')[0]}}</span>
        </div>
        <div class="body">{{item.content}}</div>
      </van-cell>
    </van-list>
    <van-button type="danger" plain @click="onLoad">加载更多</van-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      commentList: [],
      i: 1,
      text23: '',
      id: this.$route.params.id
    }
  },
  created() {
    this.$nextTick(_ => {
      this.getCommentList()
    })
  },
  methods: {
    async getCommentList() {
      const { data: res } = await this.$http(
        `/api/getcomments/${this.id}?pageindex=${this.i}`
      )
      if (res.status !== 0) {
        return this.$toast('获取评论失败！')
      }
      this.commentList.push(...res.message)
    },
    onLoad() {
      this.i++
      this.getCommentList()
    },
    async addComent() {
      const { data: res } = await this.$http.post(
        `/api/postcomment/${this.id}`,
        { content: this.text23 }
      )
      if (res.status !== 0) {
        return this.$toast('评论成功')
      }
      this.text23 = ''
      const { data: res1 } = await this.$http(
        `/api/getcomments/${this.id}?pageindex=1`
      )
      this.commentList = res1.message
    }
  }
}
</script>

<style scoped lang="less">
textarea {
  padding: 10px;
  width: 100%;
  height: 100px;
  color: #323233;
  border: 1px solid #ccc;
  box-sizing: border-box;
}
.van-button {
  width: 100%;
  margin: 5px 0;
}
.body {
  line-height: 35px;
  text-indent: 2em;
  font-size: 12px;
}
.title {
  height: 30px;
  background-color: #ccc;
  font-size: 12px;
  line-height: 30px;
  span {
    margin: 0 10px;
  }
}
</style>
